using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Catastrophe.Common.FileM
{
	public class DatConfig<T> where T : class, new()
	{
		private readonly string _path;

		public string FilePath => this._path;

		public DatConfig(string name, string path = "")
		{
			this._path = Path.Combine(string.IsNullOrEmpty(path) ? AppDomain.CurrentDomain.BaseDirectory : path, name);
		}

		public void WriteConfigData(T t)
		{
			FileUtil.WriteBinarySerialize(this._path, t);
		}

		public void WriteConfigData(List<T> list)
		{
			//using FileStream serializationStream = new FileStream(this._path, FileMode.Create);
			//BinaryFormatter binaryFormatter = new BinaryFormatter();
			//binaryFormatter.Serialize(serializationStream, list);
		}

		//public List<T> ReadConfigList()
		//{
		//	using FileStream fileStream = new FileStream(this._path, FileMode.Open);
		//	if (fileStream.Length == 0)
		//	{
		//		return null;
		//	}
  //          BinaryFormatter binaryFormatter = new BinaryFormatter();
		//	return binaryFormatter.Deserialize(fileStream) as List<T>;
		//}

		public T ReadConfigData()
		{
			return FileUtil.ReadBinaryDeserialize<T>(this._path);
		}
	}
}
